from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from configs import dburl
import pymysql
pymysql.install_as_MySQLdb()

app = Flask(__name__)
# 配置数据库的连接参数
#######################################cfg
app.config['SQLALCHEMY_DATABASE_URI'] = dburl
#######################################
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

app.app_context().push()

class Patient(db.Model):
    __tablename__ = 'patient'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255, 'utf8_general_ci'), info='姓名')
    sex = db.Column(db.String(4, 'utf8_general_ci'), info='性别')
    age = db.Column(db.String(255), info='年龄')
    pid = db.Column(db.String(255), index=True, info='患者编号') 
    borntime = db.Column(db.DateTime, info='出生日期')
    hospital = db.Column(db.String(255), info='医院名')

    def __init__(self,name,sex,age,pid,hospital,borntime):
        self.sex = sex
        self.pid = pid
        self.name = name
        self.age = age
        self.hospital = hospital
        self.borntime = borntime

class Mri(db.Model): 
    __tablename__ = 'mri'
 
    id = db.Column(db.Integer, primary_key=True)
    fid = db.Column(db.String(255), unique=True, info='MRI的唯一F号')
    patient_id = db.Column(db.ForeignKey('patient.id', ondelete='RESTRICT', onupdate='RESTRICT'), index=True, info='用外键来对应患者的id（患者主键索引）减少数据冗余')
    study_id = db.Column(db.String(255), unique=True,info='studyid')
    
    device = db.Column(db.String(255, 'utf8_general_ci'), info='成像设备')
    modality = db.Column(db.String(255, 'utf8_general_ci'), info='模态个数')
    slice = db.Column(db.Integer, info='切片个数')
    
    check_date = db.Column(db.DateTime, info='检查日期')
    complaints = db.Column(db.Text(collation='utf8_general_ci'), info='主诉')
    notes = db.Column(db.Text, info='备注')
    
    ai_invasion = db.Column(db.String(10, 'utf8_general_ci'), info='ai脑侵袭')
    ai_level = db.Column(db.String(10, 'utf8_general_ci'), info='ai分级')
    
    operation_date = db.Column(db.DateTime, info='手术日期')
    Lesion_site = db.Column(db.String(255), info='病变部位')
    arachnoid = db.Column(db.String(40, 'utf8_general_ci'), info='蛛网膜界面破坏')
    simpson = db.Column(db.String(40, 'utf8_general_ci'), info='辛普森分级')
    adhesions = db.Column(db.String(40, 'utf8_general_ci'), info='黏连')
    
    subtype = db.Column(db.String(40, 'utf8_general_ci'), info='病例亚型')
    patholog_invasion = db.Column(db.String(255),info='脑侵袭')
    pathology_level = db.Column(db.String(255),info='分级')
    pathology_valueadded = db.Column(db.String(255, 'utf8_general_ci'), info='增值指数')
    
    inv_consistence = db.Column(db.String(4, 'utf8_general_ci'), info='脑侵袭是否一致')
    level_consistence = db.Column(db.String(4, 'utf8_general_ci'), info='分级是否一致')

    available = db.Column(db.Boolean, info='是否可用')
    patient = db.relationship('Patient', primaryjoin='Mri.patient_id == Patient.id', backref='mris')

    def __init__(self,fid,patient_id,study_id,check_date,device,modality):
        self.fid = fid
        self.patient_id = patient_id
        self.study_id = study_id
        self.check_date = check_date
        self.device = device
        self.modality = modality
        # self.slice = slice
        # 此处关于slice的计算比较复杂，因为是多个模态的和，无法在单个dicom文件中直接获取，所以得在上传的时候统计slice数，所以这块可能不太准
        # 之前在后端只能上传单个dicom，现在改成上传任意dicom,因此slice的计算方法要改变，这块就得注释掉
        
        
if __name__ == '__main__':
    db.drop_all()        
    db.create_all()
